<template>
  <div class="login-container-wrapper">
    <div class="login-container">
      <div class="form-section">
        <h1 class="login-title">安全访问</h1>
        <p class="login-subtitle">请输入您的凭据以继续</p>

        <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
          <el-form-item prop="userName">
            <label for="username">用户名</label>
            <el-input
              id="username"
              v-model="loginForm.userName"
              placeholder="输入用户名"
              class="custom-input"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <label for="password">密码</label>
            <el-input
              id="password"
              type="password"
              v-model="loginForm.password"
              placeholder="输入密码"
              show-password
              class="custom-input"
            ></el-input>
          </el-form-item>

          <el-form-item class="submit-item">
            <el-button
              type="primary"
              @click="submitForm('loginForm')"
              :disabled="submitIsDisabled"
              class="login-button"
            >
              立即登录
            </el-button>
            <el-button
              type="warning"
              @click="resetForm('loginForm')"
              class="reset-button"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>

        <div class="status-indicator">
          <span class="pulse"></span> 系统运行正常
        </div>

        <div class="footer">
          &copy; 2025 运维资产管理 | personal project
        </div>
      </div>

      <div class="brand-section">
        <div class="logo-container">
          <div class="rotating-ring"></div>
          <div class="rotating-ring"></div>
          <div class="rotating-ring"></div>
          <div class="center-logo">YW</div>
        </div>

        <div class="brand-name">
          <h1>Operation <span class="track">asset</span></h1>
          <p>运维资产管理平台</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import http from '@/utils/http'
import { desEncrypt } from "@/utils/utils";

export default {
  name: "Login3",
  data() {
    return {
      videoCanPlay: false,
      fixStyle: '',
      submitIsDisabled: false,
      // 加密的key
      loginPasswordKey: '1234',
      // 登录使用的参数
      loginForm: {
        userName: '',
        password: '',
        code: '',
        // 唯一值,本次请求的唯一值
        onlyId: null,
      },
      // 数据验证
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    /**
     * 获取验证码
     */
    getVerificationCode() {
      http.post("/open/getVerificationCode").then(response => {
        let res = response.data;
        if (res.status === 'error') {
          this.$message.error(res.msg);
          return
        }
        this.loginPasswordKey = res.data.loginPasswordKey;
        this.loginForm.onlyId = res.data.keyId;

        let localStorage = window.localStorage;
        localStorage.loginPasswordKey = this.loginPasswordKey;
        this.loginPc()

      }).catch(error => {
        this.$message.error(error.message);
        this.submitIsDisabled = false;
      })
    },
    /**
     * 登录前操作
     */
    loginBefore(){
      this.getVerificationCode()
    },
    /**
     *  登录
     */
    loginPc() {
      let encryptUser = desEncrypt(this.loginForm.userName, this.loginPasswordKey);
      let encryptPassword = desEncrypt(this.loginForm.password, this.loginPasswordKey);

      http.post("/open/loginPc", {
        userName: encryptUser,
        password: encryptPassword,
        code: 'ops_code',
        onlyId: this.loginForm.onlyId

      }).then(response => {
        let res = response.data;
        if (!res.success) {
          this.$message.error(res.msg);
          return
        }

        let localStorage = window.localStorage;
        localStorage.token = res.data.tokenValue;
        localStorage.user = JSON.stringify(res.data.opsUser);
        localStorage.powerList = JSON.stringify(res.data.powerList);
        localStorage.opsRoleList = JSON.stringify(res.data.opsRoleList);

        this.$router.push({path: "/home"})

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 表单提交
     * @param formName
     */
    submitForm(formName) {
      this.submitIsDisabled = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loginBefore();
        } else {
          this.$message.error('参数填写不合法');
          this.submitIsDisabled = false;
          return false;
        }
      });
    },
    /**
     * 重置表单
     * @param formName
     */
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },

  mounted() {
    // 窗口调整大小处理
    window.onresize = () => {
      const windowWidth = document.body.clientWidth
      const windowHeight = document.body.clientHeight
      const windowAspectRatio = windowHeight / windowWidth
      let videoWidth
      let videoHeight
      if (windowAspectRatio < 0.5625) {
        videoWidth = windowWidth
        videoHeight = videoWidth * 0.5625
        this.fixStyle = {
          height: windowWidth * 0.5625 + 'px',
          width: windowWidth + 'px',
          'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
          'margin-left': 'initial'
        }
      } else {
        videoHeight = windowHeight
        videoWidth = videoHeight / 0.5625
        this.fixStyle = {
          height: windowHeight + 'px',
          width: windowHeight / 0.5625 + 'px',
          'margin-left': (windowWidth - videoWidth) / 2 + 'px',
          'margin-bottom': 'initial'
        }
      }
    }
    window.onresize();

    // 检测是否支持 localStorage
    if (!window.localStorage){
      this.$message({
        message: '您的浏览器版本太低，系统使用可能不正常',
        type: 'warning',
        duration: 0,
        showClose: true
      });
    }

    // 初始化验证码
    // this.getVerificationCode();
  }
}
</script>

<style scoped>


.login-container-wrapper {
  background: linear-gradient(135deg, #0a0f1c 0%, #152642 100%);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-container {
  display: flex;
  width: 850px;
  height: 500px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(145deg, #131e32, #0c1424);
}

/* 其他样式保持不变 */
.form-section {
  flex: 1.2;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}

.brand-section {
  flex: 0.8;
  background: linear-gradient(135deg, #1a2b4a, #0f1a2f);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 30px;
  position: relative;
  overflow: hidden;
}

/* 原有视频背景样式 */
.login-homepage-hero-module, .login-video-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.login-video-container .login-poster img, .login-video-container video {
  z-index: 0;
  position: absolute;
}

.login-video-container .login-filter {
  z-index: 1;
  position: absolute;
  background: rgba(0, 0, 0, 0.4);
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 新的登录界面样式 */
.login-container {
  display: flex;
  width: 850px;
  height: 500px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(145deg, #131e32, #0c1424);
}

.form-section {
  flex: 1.2;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}

.brand-section {
  flex: 0.8;
  background: linear-gradient(135deg, #1a2b4a, #0f1a2f);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 30px;
  position: relative;
  overflow: hidden;
}

.brand-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 80%, rgba(41, 128, 185, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(52, 152, 219, 0.1) 0%, transparent 50%);
  z-index: 0;
}

.logo-container {
  width: 180px;
  height: 180px;
  position: relative;
  margin-bottom: 30px;
  z-index: 1;
}

.rotating-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 2px solid transparent;
  border-radius: 50%;
  border-top: 2px solid #3498db;
  animation: rotate 8s linear infinite;
}

.rotating-ring:nth-child(2) {
  border-top: 2px solid #2980b9;
  animation: rotate 6s linear infinite reverse;
}

.rotating-ring:nth-child(3) {
  border-top: 2px solid #1abc9c;
  animation: rotate 10s linear infinite;
  width: 80%;
  height: 80%;
  top: 10%;
  left: 10%;
}

.center-logo {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #3498db, #1abc9c);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 14px;
  box-shadow: 0 0 20px rgba(52, 152, 219, 0.5);
}

.brand-name {
  text-align: center;
  z-index: 1;
}

.brand-name h1 {
  font-size: 28px;
  font-weight: 300;
  color: white;
  letter-spacing: 1px;
}

.brand-name .track {
  color: #3498db;
  font-weight: 600;
}

.brand-name p {
  margin-top: 10px;
  color: #a0b3d6;
  font-size: 14px;
}

.login-title {
  color: white;
  font-size: 28px;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-subtitle {
  color: #a0b3d6;
  margin-bottom: 30px;
  font-size: 15px;
}

.login-form {
  width: 100%;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  color: #b0b3d6 !important;
  font-size: 14px;
}

:deep(.custom-input .el-input__inner) {
  width: 100%;
  padding: 12px 15px;
  background-color: #1e2a3e !important;
  border: 1px solid #2c3e50 !important;
  border-radius: 4px;
  color: white !important;
  font-size: 15px;
  transition: all 0.3s;
}

:deep(.custom-input .el-input__inner:focus) {
  outline: none;
  border-color: #3498db !important;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2) !important;
}

:deep(.custom-input .el-input__inner::placeholder) {
  color: #6c7a95 !important;
}

.submit-item {
  margin-top: 10px;
}

:deep(.login-button) {
  background: linear-gradient(to right, #3498db, #2980b9) !important;
  border: none !important;
  padding: 12px 20px !important;
  border-radius: 4px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  transition: all 0.3s !important;
  box-shadow: 0 4px 10px rgba(52, 152, 219, 0.3) !important;
}

:deep(.login-button:hover) {
  transform: translateY(-2px) !important;
  box-shadow: 0 6px 15px rgba(52, 152, 219, 0.4) !important;
}

:deep(.login-button:disabled) {
  opacity: 0.6 !important;
  transform: none !important;
}

:deep(.reset-button) {
  background: linear-gradient(to right, #e67e22, #d35400) !important;
  border: none !important;
  padding: 12px 20px !important;
  border-radius: 4px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  transition: all 0.3s !important;
}

:deep(.reset-button:hover) {
  transform: translateY(-2px) !important;
  box-shadow: 0 6px 15px rgba(230, 126, 34, 0.4) !important;
}

.status-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 15px;
  font-size: 12px;
  color: #2ecc71;
}

.pulse {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #2ecc71;
  margin-right: 8px;
  box-shadow: 0 0 0 rgba(46, 204, 113, 0.4);
  animation: pulse 2s infinite;
}

.footer {
  margin-top: 20px;
  text-align: center;
  font-size: 12px;
  color: #6c7a95;
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(46, 204, 113, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(46, 204, 113, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(46, 204, 113, 0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    height: auto;
    width: 90%;
    max-width: 400px;
  }

  .brand-section {
    order: -1;
    padding: 30px;
  }

  .form-section {
    padding: 30px;
  }

  .logo-container {
    width: 140px;
    height: 140px;
  }
}
</style>
